package org.ysu.shoeShopping.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.ysu.shoeShopping.dao.ReceivesDao;
import org.ysu.shoeShopping.entity.Receives;
import org.ysu.shoeShopping.service.ReceivesService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 收货地址表(Receives)表服务实现类
 *
 * @author makejava
 * @since 2025-09-13 16:59:30
 */
@Service("receivesService")
public class ReceivesServiceImpl implements ReceivesService
{
    @Resource
    private ReceivesDao receivesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param recid 主键
     * @return 实例对象
     */
    @Override
    public Receives queryById(Integer recid) {
        return this.receivesDao.queryById(recid);
    }

    /**
     * 分页查询
     *
     * @param receives 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Receives> queryByPage(Receives receives, PageRequest pageRequest) {
        long total = this.receivesDao.count(receives);
        return new PageImpl<>(this.receivesDao.queryAllByLimit(receives, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param receives 实例对象
     * @return 实例对象
     */
    @Override
    public Receives insert(Receives receives) {
        this.receivesDao.insert(receives);
        return receives;
    }

    /**
     * 修改数据
     *
     * @param receives 实例对象
     * @return 实例对象
     */
    @Override
    public Receives update(Receives receives) {
        this.receivesDao.update(receives);
        return this.queryById(receives.getRecid());
    }

    /**
     * 通过主键删除数据
     *
     * @param recid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer recid) {
        return this.receivesDao.deleteById(recid) > 0;
    }

    /**
     * 根据用户ID查询收货地址列表
     *
     * @param userId 用户ID
     * @return 收货地址列表
     */
    @Override
    public List<Receives> findByUserId(Integer userId) {
        return this.receivesDao.findByUserId(userId);
    }
}
